var express = require('express');
var fs = require('fs');

module.exports = function(app) {
    var router = express.Router();

    router.post('/', function(req, res, next) {
        var success = true;
        var msg = null;
        var files = req.body.files;

        for (i in files) {
            console.log('[FileServer] Uploading: ' + files[i].filename);
            try {
                fs.writeFileSync('./files/' + files[i].filename, files[i].file, 'base64');
            } catch (ex) {
                success = false;
                msg = ex;
            }
        }

        res.send({ success: success, errorMsg: msg });
    });

    app.use('/write', router);
}